/**
 * 
 */
package com.nsoft.server.serviceImpl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.nsoft.server.master.beans.AccountMaster;
import com.nsoft.server.master.beans.PartyMaster;
import com.nsoft.server.service.dao.AccountDao;
import com.nsoft.server.service.dao.PartyDao;
import com.nsoft.server.service.main.AccountService;
import com.nsoft.thickclient.beanUtils.BeanUtilities;
import com.nsoft.thickclient.trackMove.intr.account.Account;
import com.nsoft.thickclient.trackMove.intr.account.ThinAccount;

/**
 * @author venkat
 * 
 */
public class AccountServiceImpl implements AccountService {

	public AccountDao getAccountDao() {
		return accountDao;
	}

	public void setAccountDao(AccountDao accountDao) {
		this.accountDao = accountDao;
	}

	public AccountDao accountDao;

	public PartyDao partyDao;

	public PartyDao getPartyDao() {
		return partyDao;
	}

	public void setPartyDao(PartyDao partyDao) {
		this.partyDao = partyDao;
	}

	@Transactional
	public Account saveAccount(Account account) {
		AccountMaster accountMaster = null;
		if (account.getId() == null) {
			accountMaster = new AccountMaster();
		} else {
			accountMaster = accountDao.findById(new Integer(account.getId()
					.intValue()), false);
		}

		String[] sourceNames = new String[] { "number", "name", "type",
				"creationDate", "openDate", "openingBalance",
				"currentBalance", "limits", "updationDate", "selectable",
				"remarks" };
		String[] destinationNames = new String[] { "accCharCode",
				"accCharName", "accNumAtype", "accDateCreated",
				"accDateOpendate", "accCurOpenbal", "accCurBalance",
				"accCurLimits",  "accDateUpdatedon",
				"accCharSelectable" ,"accCharNotes"};
		BeanUtilities.copyProerties(account, accountMaster, sourceNames,
				destinationNames);
		PartyMaster partyMaster = partyDao.findById(new Integer(account.getPartyId()
				.intValue()), false);
		accountMaster.setParty(partyMaster);
		accountDao.makePersistent(accountMaster);
		return account;
	}

	@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
	public AccountMaster getAccountDetails(Long id) {
		return accountDao.findById(new Integer(id.intValue()), false);
	}
	
	@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
	public List<ThinAccount> getAllThinAccounts(ThinAccount thinAccount){
		return accountDao.getAllThinAccounts(thinAccount);
	}
	@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
	public List<Account> searchAccount(Account account){
		List<AccountMaster> list = accountDao.searchAccount(account);
		List<Account> accountList = new ArrayList<Account>();
		
		String[]  destinationNames= new String[] { "number", "name", "type",
				"creationDate", "openDate", "openingBalance",
				"currentBalance", "limits", "updationDate", "selectable",
				"remarks" };
		String[] sourceNames = new String[] { "accCharCode",
				"accCharName", "accNumAtype", "accDateCreated",
				"accDateOpendate", "accCurOpenbal", "accCurBalance",
				"accCurLimits",  "accDateUpdatedon",
				"accCharSelectable" ,"accCharNotes"};
		
		for (AccountMaster accountMaster : list) {
			
			Account account2 = new Account();
			
			account2.setId(new Long(accountMaster.getAccNumId()));
			BeanUtilities.copyProerties(accountMaster, account2, sourceNames,
					destinationNames);
			account2.setPartyId(new Long(accountMaster.getParty().getPartyNumId()));
			accountList.add(account2);
		}
		return accountList;
	}
	
}
